<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExport\HotelExpoter;
use App\Admin\Extensions\Tools\ImportButton;
use App\Models\Area;
use App\Models\Hotel;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class HotelController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('星级酒店信息')
            ->body($this->grid());
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id,Content $content)
    {
        return $content
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Hotel);
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->tools(function ($tools) {
            $tools->append(new ImportButton('hotels'));
        });
        $grid->exporter(new HotelExpoter());
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->like('title','名称');
            $filter->equal('area_id','区县选择')->select(Area::all()->pluck('title', 'id'));
        });
        $grid->id('ID')->sortable();
        $grid->area_id('区县')->using(Area::all()->pluck('title','id')->toArray());//->using( )
        $grid->title('名称');
        $grid->level('星级')->using(config('options.hotel_star'));//
        $grid->address('地址');
        $grid->tel('电话');
        $grid->updated_at('更新于');
        $grid->status('运营状态')->display(function ($val){
            return $val?config('options.status')[$val]:'运营中';
        });
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Hotel);
        $form->display('id', 'ID');
        $form->select('area_id','区县')->options(Area::all()->pluck('title','id'));
        $form->select('level','星级')->options(config('options.hotel_star'));
        $form->text('title','名称');
        $form->text('address','地址');
        $form->tags('latlng','坐标');
        $form->text('tel','联系电话');
        return $form;
    }
}
